/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ProductCatogoriesAddEditPnl.java
 *
 * Created on Jul 17, 2011, 10:14:56 AM
 */
package com.viweb.storemanager.ui.product;

import com.viweb.storemanager.core.CursorManager;
import com.viweb.storemanager.core.MyListCellRenderer;
import com.viweb.storemanager.core.ObjectComboBoxModel;
import com.viweb.storemanager.core.OptionPane;
import com.viweb.storemanager.dao.Biz;
import com.viweb.storemanager.entity.BaseEntity;
import com.viweb.storemanager.entity.Category;
import com.viweb.storemanager.entity.TypeCategory;
import com.viweb.storemanager.ui.customer.CustomerAddEditPnl;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author qhvic
 */
public class ProductCatogoriesAddEditPnl extends javax.swing.JPanel {

    private ProductCatogoriesPnl parent;
    private ArrayList<TypeCategory> typeCategorys;
    private Category category;

    /** Creates new form ProductCatogoriesAddEditPnl */
    public ProductCatogoriesAddEditPnl() {
        initComponents();
    }

    public ProductCatogoriesAddEditPnl(ProductCatogoriesPnl parent) {
        this.typeCategorys = Biz.getInstance().selectAllTypeCategory();
        initComponents();
        this.parent = parent;
    }

    public void assignCategory(Category category) {
        this.category = category;

        if (this.category.getId() != -1) {
            this.txtName.setText(category.getName());
            this.cbbTypes.setSelectedIndex(Biz.getObjectIndex((BaseEntity) this.category.getType(), this.typeCategorys));
            this.txtNotes.setText(category.getNote());
            this.lblDateModifierEdit.setText(category.getDatemodifies().toString());
            if (category.getStatus() != 0) {
                this.radioYes.setSelected(true);
            } else {
                this.radioNo.setSelected(true);
            }
        } else {
            this.txtName.setText(null);
            this.radioYes.setSelected(true);
            this.txtNotes.setText(null);
            this.lblDateModifierEdit.setText(null);
        }
    }

    public void setEditableMode(boolean editable) {
        if (editable) {
            this.txtName.requestFocus();
        }
        this.cbbTypes.setEditable(editable);
        this.txtName.setEditable(editable);
        this.txtNotes.setEditable(editable);
        this.btnSave.setEnabled(editable);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        btnGroupStatus = new javax.swing.ButtonGroup();
        lblName = new javax.swing.JLabel();
        lblNotes = new javax.swing.JLabel();
        lblDateModifier = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        lblEmpty = new javax.swing.JLabel();
        scpNotes = new javax.swing.JScrollPane();
        txtNotes = new javax.swing.JTextPane();
        btnSave = new javax.swing.JButton();
        lblDateModifierEdit = new javax.swing.JLabel();
        lblTypes = new javax.swing.JLabel();
        cbbTypes = new javax.swing.JComboBox();
        lblStatus = new javax.swing.JLabel();
        radioYes = new javax.swing.JRadioButton();
        radioNo = new javax.swing.JRadioButton();

        setLayout(new java.awt.GridBagLayout());

        lblName.setText("Loại mặt hàng:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 0);
        add(lblName, gridBagConstraints);

        lblNotes.setText("Ghi chú:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 0);
        add(lblNotes, gridBagConstraints);

        lblDateModifier.setText("Ngày cập nhật:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 0);
        add(lblDateModifier, gridBagConstraints);

        txtName.setEditable(false);
        txtName.setMinimumSize(new java.awt.Dimension(200, 25));
        txtName.setPreferredSize(new java.awt.Dimension(200, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        add(txtName, gridBagConstraints);

        lblEmpty.setMinimumSize(new java.awt.Dimension(5, 50));
        lblEmpty.setPreferredSize(new java.awt.Dimension(5, 50));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(lblEmpty, gridBagConstraints);

        txtNotes.setEditable(false);
        scpNotes.setViewportView(txtNotes);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        add(scpNotes, gridBagConstraints);

        btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/viweb/storemanager/imgae/save.png"))); // NOI18N
        btnSave.setText("Lưu");
        btnSave.setToolTipText("Lưu thể loại vừa thêm mới hay hiệu chỉnh");
        btnSave.setEnabled(false);
        btnSave.setMaximumSize(new java.awt.Dimension(80, 26));
        btnSave.setMinimumSize(new java.awt.Dimension(80, 26));
        btnSave.setPreferredSize(new java.awt.Dimension(80, 26));
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(btnSave, gridBagConstraints);

        lblDateModifierEdit.setMaximumSize(new java.awt.Dimension(200, 14));
        lblDateModifierEdit.setMinimumSize(new java.awt.Dimension(200, 14));
        lblDateModifierEdit.setPreferredSize(new java.awt.Dimension(200, 14));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(lblDateModifierEdit, gridBagConstraints);

        lblTypes.setText("Ngành mặt hàng:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 0);
        add(lblTypes, gridBagConstraints);

        cbbTypes.setMinimumSize(new java.awt.Dimension(200, 22));
        cbbTypes.setPreferredSize(new java.awt.Dimension(200, 22));
        ObjectComboBoxModel model = new ObjectComboBoxModel(this.typeCategorys);
        cbbTypes.setModel(model);
        cbbTypes.setRenderer(new MyListCellRenderer(22));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        add(cbbTypes, gridBagConstraints);

        lblStatus.setText("Tình trạng:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 0);
        add(lblStatus, gridBagConstraints);

        btnGroupStatus.add(radioYes);
        radioYes.setText("Có");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        add(radioYes, gridBagConstraints);

        btnGroupStatus.add(radioNo);
        radioNo.setText("Không");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        add(radioNo, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        // TODO add your handling code here:
        try {
            // TODO add your handling code here:
            this.category.setName(this.txtName.getText().trim());
            this.category.setType((TypeCategory) this.cbbTypes.getSelectedItem());
            this.category.setNote(this.txtNotes.getText().trim());
            this.category.setStatus((byte) (radioYes.isSelected() ? 1 : 0));
            boolean status = false;
            if (this.category.getId() != -1) {
                status = Biz.getInstance().insertCategory(category);
            } else {
                status = Biz.getInstance().updateCategory(category);
            }
            // Nếu cập nhật (thêm mới hoặc sữa thông tin) không thành công
            if (status) {
                OptionPane.show(JOptionPane.getFrameForComponent(ProductCatogoriesAddEditPnl.this), "Cập nhật không thành công! " + Biz.getInstance().getLastError(), "Thông báo", OptionPane.MsgTYPE_WARNING);
                // Nếu cập nhật thành công
            } else {
                if (this.category.getId() == -1) {
                    // Set id cho quốc gia vừa mới cập nhật (chỉ có ý nghĩa trong trường hợp thêm mới)
                    //this.customer.setId(id);
                    OptionPane.show(JOptionPane.getFrameForComponent(ProductCatogoriesAddEditPnl.this), "Thêm mới loại sản phẩm thành công!", "Thông báo");
                } else {
                    OptionPane.show(JOptionPane.getFrameForComponent(ProductCatogoriesAddEditPnl.this), "Cập nhật loại sản phẩm thành công!", "Thông báo");
                }
                this.parent.updateComponentsUI(this.category);
            }
            CursorManager.setDefaultCursor(this);
        } catch (Exception ex) {
            Logger.getLogger(CustomerAddEditPnl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnSaveActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup btnGroupStatus;
    private javax.swing.JButton btnSave;
    private javax.swing.JComboBox cbbTypes;
    private javax.swing.JLabel lblDateModifier;
    private javax.swing.JLabel lblDateModifierEdit;
    private javax.swing.JLabel lblEmpty;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblNotes;
    private javax.swing.JLabel lblStatus;
    private javax.swing.JLabel lblTypes;
    private javax.swing.JRadioButton radioNo;
    private javax.swing.JRadioButton radioYes;
    private javax.swing.JScrollPane scpNotes;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextPane txtNotes;
    // End of variables declaration//GEN-END:variables
}
